home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / kernel / exit.c next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  14.9 KB  |  618 lines

  1. /*
  2.  *  linux/kernel/exit.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. #define DEBUG_PROC_TREE
  12.  
  13. #include <asm/system.h>
  14. #include <asm/segment.h>
  15.  
  16. #include <linux/wait.h>
  17. #include <linux/errno.h>
  18. #include <linux/signal.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/resource.h>
  22. #include <linux/mm.h>
  23. #include <linux/tty.h>
  24. #include <linux/malloc.h>
  25.  
  26. extern void shm_exit (void);
  27. extern void sem_exit (void);
  28.  
  29. int getrusage(struct task_struct *, int, struct rusage *);
  30.  
  31. static int generate(unsigned long sig, struct task_struct * p)
  32. {
  33.     unsigned long mask = 1 << (sig-1);
  34.     struct sigaction * sa = sig + p->sigaction - 1;
  35.  
  36.     /* always generate signals for traced processes ??? */
  37.     if (p->flags & PF_PTRACED) {
  38.         p->signal |= mask;
  39.         return 1;
  40.     }
  41.     /* don't bother with ignored signals (but SIGCHLD is special) */
  42.     if (sa->sa_handler == SIG_IGN && sig != SIGCHLD)
  43.         return 0;
  44.     /* some signals are ignored by default.. (but SIGCONT already did its deed) */
  45.     if ((sa->sa_handler == SIG_DFL) &&
  46.         (sig == SIGCONT || sig == SIGCHLD || sig == SIGWINCH))
  47.         return 0;
  48.     p->signal |= mask;
  49.     return 1;
  50. }
  51.  
  52. int send_sig(unsigned long sig,struct task_struct * p,int priv)
  53. {
  54. #ifdef DEBUG
  55.     printk ("send_sig called\n");
  56. #endif
  57.     if (sig == 11)
  58.         printk ("send_sig(SEGV) from %#lx\n", ((ulong *)&sig)[-1]);
  59.     
  60.     if (!p || sig > 32)
  61.         return -EINVAL;
  62.     if (!priv && ((sig != SIGCONT) || (current->session != p->session)) &&
  63.         (current->euid != p->euid) && (current->uid != p->uid) && !suser())
  64.         return -EPERM;
  65.     if (!sig)
  66.         return 0;
  67.     if ((sig == SIGKILL) || (sig == SIGCONT)) {
  68.         if (p->state == TASK_STOPPED)
  69.             p->state = TASK_RUNNING;
  70.         p->exit_code = 0;
  71.         p->signal &= ~( (1<<(SIGSTOP-1)) | (1<<(SIGTSTP-1)) |
  72.                 (1<<(SIGTTIN-1)) | (1<<(SIGTTOU-1)) );
  73.     }
  74.     /* Depends on order SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU */
  75.     if ((sig >= SIGSTOP) && (sig <= SIGTTOU))
  76.         p->signal &= ~(1<<(SIGCONT-1));
  77.     /* Actually deliver the signal */
  78.     generate(sig,p);
  79.     return 0;
  80. }
  81.  
  82. void notify_parent(struct task_struct * tsk)
  83. {
  84.     if (tsk->p_pptr == task[1])
  85.         tsk->exit_signal = SIGCHLD;
  86.     send_sig(tsk->exit_signal, tsk->p_pptr, 1);
  87.     wake_up_interruptible(&tsk->p_pptr->wait_chldexit);
  88. }
  89.  
  90. void release(struct task_struct * p)
  91. {
  92.     int i;
  93.  
  94.     if (!p)
  95.         return;
  96.     if (p == current) {
  97.         printk("task releasing itself\n");
  98.         return;
  99.     }
  100.     for (i=1 ; i<NR_TASKS ; i++)
  101.         if (task[i] == p) {
  102.             task[i] = NULL;
  103.             REMOVE_LINKS(p);
  104.             free_page(p->kernel_stack_page);
  105.             free_page((long) p);
  106.             return;
  107.         }
  108.     panic("trying to release non-existent task");
  109. }
  110.  
  111. #ifdef DEBUG_PROC_TREE
  112. /*
  113.  * Check to see if a task_struct pointer is present in the task[] array
  114.  * Return 0 if found, and 1 if not found.
  115.  */
  116. int bad_task_ptr(struct task_struct *p)
  117. {
  118.     int    i;
  119.  
  120.     if (!p)
  121.         return 0;
  122.     for (i=0 ; i<NR_TASKS ; i++)
  123.         if (task[i] == p)
  124.             return 0;
  125.     return 1;
  126. }
  127.  
  128. /*
  129.  * This routine scans the pid tree and make sure the rep invarient still
  130.  * holds.  Used for debugging only, since it's very slow....
  131.  *
  132.  * It looks a lot scarier than it really is.... we're doing nothing more
  133.  * than verifying the doubly-linked list found in p_ysptr and p_osptr,
  134.  * and checking it corresponds with the process tree defined by p_cptr and
  135.  * p_pptr;
  136.  */
  137. void audit_ptree(void)
  138. {
  139.     int    i;
  140.  
  141.     for (i=1 ; i<NR_TASKS ; i++) {
  142.         if (!task[i])
  143.             continue;
  144.         if (bad_task_ptr(task[i]->p_pptr))
  145.             printk("Warning, pid %d's parent link is bad\n",
  146.                 task[i]->pid);
  147.         if (bad_task_ptr(task[i]->p_cptr))
  148.             printk("Warning, pid %d's child link is bad\n",
  149.                 task[i]->pid);
  150.         if (bad_task_ptr(task[i]->p_ysptr))
  151.             printk("Warning, pid %d's ys link is bad\n",
  152.                 task[i]->pid);
  153.         if (bad_task_ptr(task[i]->p_osptr))
  154.             printk("Warning, pid %d's os link is bad\n",
  155.                 task[i]->pid);
  156.         if (task[i]->p_pptr == task[i])
  157.             printk("Warning, pid %d parent link points to self\n",
  158.                 task[i]->pid);
  159.         if (task[i]->p_cptr == task[i])
  160.             printk("Warning, pid %d child link points to self\n",
  161.                 task[i]->pid);
  162.         if (task[i]->p_ysptr == task[i])
  163.             printk("Warning, pid %d ys link points to self\n",
  164.                 task[i]->pid);
  165.         if (task[i]->p_osptr == task[i])
  166.             printk("Warning, pid %d os link points to self\n",
  167.                 task[i]->pid);
  168.         if (task[i]->p_osptr) {
  169.             if (task[i]->p_pptr != task[i]->p_osptr->p_pptr)
  170.                 printk(
  171.             "Warning, pid %d older sibling %d parent is %d\n",
  172.                 task[i]->pid, task[i]->p_osptr->pid,
  173.                 task[i]->p_osptr->p_pptr->pid);
  174.             if (task[i]->p_osptr->p_ysptr != task[i])
  175.                 printk(
  176.         "Warning, pid %d older sibling %d has mismatched ys link\n",
  177.                 task[i]->pid, task[i]->p_osptr->pid);
  178.         }
  179.         if (task[i]->p_ysptr) {
  180.             if (task[i]->p_pptr != task[i]->p_ysptr->p_pptr)
  181.                 printk(
  182.             "Warning, pid %d younger sibling %d parent is %d\n",
  183.                 task[i]->pid, task[i]->p_osptr->pid,
  184.                 task[i]->p_osptr->p_pptr->pid);
  185.             if (task[i]->p_ysptr->p_osptr != task[i])
  186.                 printk(
  187.         "Warning, pid %d younger sibling %d has mismatched os link\n",
  188.                 task[i]->pid, task[i]->p_ysptr->pid);
  189.         }
  190.         if (task[i]->p_cptr) {
  191.             if (task[i]->p_cptr->p_pptr != task[i])
  192.                 printk(
  193.             "Warning, pid %d youngest child %d has mismatched parent link\n",
  194.                 task[i]->pid, task[i]->p_cptr->pid);
  195.             if (task[i]->p_cptr->p_ysptr)
  196.                 printk(
  197.             "Warning, pid %d youngest child %d has non-NULL ys link\n",
  198.                 task[i]->pid, task[i]->p_cptr->pid);
  199.         }
  200.     }
  201. }
  202. #endif /* DEBUG_PROC_TREE */
  203.  
  204. /*
  205.  * This checks not only the pgrp, but falls back on the pid if no
  206.  * satisfactory prgp is found. I dunno - gdb doesn't work correctly
  207.  * without this...
  208.  */
  209. int session_of_pgrp(int pgrp)
  210. {
  211.     struct task_struct *p;
  212.     int fallback;
  213.  
  214.     fallback = -1;
  215.     for_each_task(p) {
  216.         if (p->session <= 0)
  217.             continue;
  218.         if (p->pgrp == pgrp)
  219.             return p->session;
  220.         if (p->pid == pgrp)
  221.             fallback = p->session;
  222.     }
  223.     return fallback;
  224. }
  225.  
  226. /*
  227.  * kill_pg() sends a signal to a process group: this is what the tty
  228.  * control characters do (^C, ^Z etc)
  229.  */
  230. int kill_pg(int pgrp, int sig, int priv)
  231. {
  232.     struct task_struct *p;
  233.     int err,retval = -ESRCH;
  234.     int found = 0;
  235.  
  236.     if (sig<0 || sig>32 || pgrp<=0)
  237.         return -EINVAL;
  238.     for_each_task(p) {
  239.         if (p->pgrp == pgrp) {
  240.             if ((err = send_sig(sig,p,priv)) != 0)
  241.                 retval = err;
  242.             else
  243.                 found++;
  244.         }
  245.     }
  246.     return(found ? 0 : retval);
  247. }
  248.  
  249. /*
  250.  * kill_sl() sends a signal to the session leader: this is used
  251.  * to send SIGHUP to the controlling process of a terminal when
  252.  * the connection is lost.
  253.  */
  254. int kill_sl(int sess, int sig, int priv)
  255. {
  256.     struct task_struct *p;
  257.     int err,retval = -ESRCH;
  258.     int found = 0;
  259.  
  260.     if (sig<0 || sig>32 || sess<=0)
  261.         return -EINVAL;
  262.     for_each_task(p) {
  263.         if (p->session == sess && p->leader) {
  264.             if ((err = send_sig(sig,p,priv)) != 0)
  265.                 retval = err;
  266.             else
  267.                 found++;
  268.         }
  269.     }
  270.     return(found ? 0 : retval);
  271. }
  272.  
  273. int kill_proc(int pid, int sig, int priv)
  274. {
  275.     struct task_struct *p;
  276.  
  277.     if (sig<0 || sig>32)
  278.         return -EINVAL;
  279.      for_each_task(p) {
  280.          if (p && p->pid == pid)
  281.              return send_sig(sig,p,priv);
  282.      }
  283.     return(-ESRCH);
  284. }
  285.  
  286. /*
  287.  * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  288.  * is probably wrong.  Should make it like BSD or SYSV.
  289.  */
  290. asmlinkage int sys_kill(int pid,int sig)
  291. {
  292.     int err, retval = 0, count = 0;
  293.  
  294.     if (!pid)
  295.         return(kill_pg(current->pgrp,sig,0));
  296.     if (pid == -1) {
  297.         struct task_struct * p;
  298.         for_each_task(p) {
  299.             if (p->pid > 1 && p != current) {
  300.                 ++count;
  301.                 if ((err = send_sig(sig,p,0)) != -EPERM)
  302.                     retval = err;
  303.             }
  304.         }
  305.         return(count ? retval : -ESRCH);
  306.     }
  307.     if (pid < 0)
  308.         return(kill_pg(-pid,sig,0));
  309.     /* Normal kill */
  310.     return(kill_proc(pid,sig,0));
  311. }
  312.  
  313. /*
  314.  * Determine if a process group is "orphaned", according to the POSIX
  315.  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
  316.  * by terminal-generated stop signals.    Newly orphaned process groups are
  317.  * to receive a SIGHUP and a SIGCONT.
  318.  *
  319.  * "I ask you, have you ever known what it is to be an orphan?"
  320.  */
  321. int is_orphaned_pgrp(int pgrp)
  322. {
  323.     struct task_struct *p;
  324.  
  325.     for_each_task(p) {
  326.         if ((p->pgrp != pgrp) || 
  327.             (p->state == TASK_ZOMBIE) ||
  328.             (p->p_pptr->pid == 1))
  329.             continue;
  330.         if ((p->p_pptr->pgrp != pgrp) &&
  331.             (p->p_pptr->session == p->session))
  332.             return 0;
  333.     }
  334.     return(1);      /* (sighing) "Often!" */
  335. }
  336.  
  337. static int has_stopped_jobs(int pgrp)
  338. {
  339.     struct task_struct * p;
  340.  
  341.     for_each_task(p) {
  342.         if (p->pgrp != pgrp)
  343.             continue;
  344.         if (p->state == TASK_STOPPED)
  345.             return(1);
  346.     }
  347.     return(0);
  348. }
  349.  
  350. static void forget_original_parent(struct task_struct * father)
  351. {
  352.     struct task_struct * p;
  353.  
  354.     for_each_task(p) {
  355.         if (p->p_opptr == father)
  356.             if (task[1])
  357.                 p->p_opptr = task[1];
  358.             else
  359.                 p->p_opptr = task[0];
  360.     }
  361. }
  362.  
  363. NORET_TYPE void do_exit(long code)
  364. {
  365.     struct task_struct *p;
  366.     int i;
  367. #ifdef DEBUG
  368.     printk ("do_exit called\n");
  369. #endif
  370. fake_volatile:
  371.     if (current->semun)
  372.         sem_exit();
  373.     if (current->shm)
  374.         shm_exit();
  375.     /*
  376.      * disable interrupts so that the freed pages will not be re-used.
  377.      * This is so that the kernel stack can still be used.
  378.      */
  379.     cli();
  380.     free_page_tables(current);
  381.     for (i=0 ; i<NR_OPEN ; i++)
  382.         if (current->filp[i])
  383.             sys_close(i);
  384.     forget_original_parent(current);
  385.     iput(current->pwd);
  386.     current->pwd = NULL;
  387.     iput(current->root);
  388.     current->root = NULL;
  389.     iput(current->executable);
  390.     current->executable = NULL;
  391.     /* Release all of the old mmap stuff. */
  392.  
  393.     {
  394.         struct vm_area_struct * mpnt, *mpnt1;
  395.         mpnt = current->mmap;
  396.         current->mmap = NULL;
  397.         while (mpnt) {
  398.             mpnt1 = mpnt->vm_next;
  399.             if (mpnt->vm_ops && mpnt->vm_ops->close)
  400.                 mpnt->vm_ops->close(mpnt);
  401.             kfree(mpnt);
  402.             mpnt = mpnt1;
  403.         }
  404.     }
  405.  
  406. #ifdef __i386__
  407.     if (current->ldt) {
  408.         free_page((unsigned long) current->ldt);
  409.         current->ldt = NULL;
  410.         for (i=1 ; i<NR_TASKS ; i++) {
  411.             if (task[i] == current) {
  412.                 set_ldt_desc(gdt+(i<<1)+FIRST_LDT_ENTRY, &default_ldt, 1);
  413.                 load_ldt(i);
  414.             }
  415.         }
  416.     }
  417. #endif
  418.  
  419.     current->state = TASK_ZOMBIE;
  420.     current->exit_code = code;
  421.     current->rss = 0;
  422.  
  423.     /*
  424.      * Check to see if any process groups have become orphaned
  425.      * as a result of our exiting, and if they have any stopped
  426.      * jobs, send them a SIGUP and then a SIGCONT.    (POSIX 3.2.2.2)
  427.      *
  428.      * Case i: Our father is in a different pgrp than we are
  429.      * and we were the only connection outside, so our pgrp
  430.      * is about to become orphaned.
  431.      */
  432.     if ((current->p_pptr->pgrp != current->pgrp) &&
  433.         (current->p_pptr->session == current->session) &&
  434.         is_orphaned_pgrp(current->pgrp) &&
  435.         has_stopped_jobs(current->pgrp)) {
  436.         kill_pg(current->pgrp,SIGHUP,1);
  437.         kill_pg(current->pgrp,SIGCONT,1);
  438.     }
  439.     /* Let father know we died */
  440.     notify_parent(current);
  441.  
  442.     /*
  443.      * This loop does two things:
  444.      *
  445.      * A.  Make init inherit all the child processes
  446.      * B.  Check to see if any process groups have become orphaned
  447.      *    as a result of our exiting, and if they have any stopped
  448.      *    jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
  449.      */
  450.     while ((p = current->p_cptr) != NULL) {
  451.         current->p_cptr = p->p_osptr;
  452.         p->p_ysptr = NULL;
  453.         p->flags &= ~(PF_PTRACED|PF_TRACESYS);
  454.         if (task[1])
  455.             p->p_pptr = task[1];
  456.         else
  457.             p->p_pptr = task[0];
  458.         p->p_osptr = p->p_pptr->p_cptr;
  459.         p->p_osptr->p_ysptr = p;
  460.         p->p_pptr->p_cptr = p;
  461.         if (p->state == TASK_ZOMBIE)
  462.             notify_parent(p);
  463.         /*
  464.          * process group orphan check
  465.          * Case ii: Our child is in a different pgrp
  466.          * than we are, and it was the only connection
  467.          * outside, so the child pgrp is now orphaned.
  468.          */
  469.         if ((p->pgrp != current->pgrp) &&
  470.             (p->session == current->session) &&
  471.             is_orphaned_pgrp(p->pgrp) &&
  472.             has_stopped_jobs(p->pgrp)) {
  473.             kill_pg(p->pgrp,SIGHUP,1);
  474.             kill_pg(p->pgrp,SIGCONT,1);
  475.         }
  476.     }
  477.  
  478.     if (current->leader) {
  479.         struct task_struct *p;
  480.         struct tty_struct *tty;
  481.  
  482.         if (current->tty >= 0) {
  483.             tty = TTY_TABLE(current->tty);
  484.             if (tty) {
  485.                 if (tty->pgrp > 0)
  486.                     kill_pg(tty->pgrp, SIGHUP, 1);
  487.                 tty->pgrp = -1;
  488.                 tty->session = 0;
  489.             }
  490.         }
  491.         for_each_task(p) {
  492.             if (p->session == current->session)
  493.                 p->tty = -1;
  494.         }
  495.     }
  496.     if (last_task_used_math == current)
  497.         last_task_used_math = NULL;
  498. #ifdef DEBUG_PROC_TREE
  499.     audit_ptree();
  500. #endif
  501.     schedule();
  502. /*
  503.  * In order to get rid of the "volatile function does return" message
  504.  * I did this little loop that confuses gcc to think do_exit really
  505.  * is volatile. In fact it's schedule() that is volatile in some
  506.  * circumstances: when current->state = ZOMBIE, schedule() never
  507.  * returns.
  508.  *
  509.  * In fact the natural way to do all this is to have the label and the
  510.  * goto right after each other, but I put the fake_volatile label at
  511.  * the start of the function just in case something /really/ bad
  512.  * happens, and the schedule returns. This way we can try again. I'm
  513.  * not paranoid: it's just that everybody is out to get me.
  514.  */
  515.     goto fake_volatile;
  516. }
  517.  
  518. asmlinkage int sys_exit(int error_code)
  519. {
  520.     do_exit((error_code&0xff)<<8);
  521. }
  522.  
  523. asmlinkage int sys_wait4(pid_t pid,unsigned long * stat_addr, int options, struct rusage * ru)
  524. {
  525.     int flag, retval;
  526.     struct wait_queue wait = { current, NULL };
  527.     struct task_struct *p;
  528.  
  529.     if (stat_addr) {
  530.         flag = verify_area(VERIFY_WRITE, stat_addr, 4);
  531.         if (flag)
  532.             return flag;
  533.     }
  534.     add_wait_queue(¤t->wait_chldexit,&wait);
  535. repeat:
  536.     flag=0;
  537.     for (p = current->p_cptr ; p ; p = p->p_osptr) {
  538.         if (pid>0) {
  539.             if (p->pid != pid)
  540.                 continue;
  541.         } else if (!pid) {
  542.             if (p->pgrp != current->pgrp)
  543.                 continue;
  544.         } else if (pid != -1) {
  545.             if (p->pgrp != -pid)
  546.                 continue;
  547.         }
  548.         /* wait for cloned processes iff the __WCLONE flag is set */
  549.         if ((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  550.             continue;
  551.         flag = 1;
  552.         switch (p->state) {
  553.             case TASK_STOPPED:
  554.                 if (!p->exit_code)
  555.                     continue;
  556.                 if (!(options & WUNTRACED) && !(p->flags & PF_PTRACED))
  557.                     continue;
  558.                 if (stat_addr)
  559.                     put_fs_long((p->exit_code << 8) | 0x7f,
  560.                         stat_addr);
  561.                 p->exit_code = 0;
  562.                 if (ru != NULL)
  563.                     getrusage(p, RUSAGE_BOTH, ru);
  564.                 retval = p->pid;
  565.                 goto end_wait4;
  566.             case TASK_ZOMBIE:
  567.                 current->cutime += p->utime + p->cutime;
  568.                 current->cstime += p->stime + p->cstime;
  569.                 current->cmin_flt += p->min_flt + p->cmin_flt;
  570.                 current->cmaj_flt += p->maj_flt + p->cmaj_flt;
  571.                 if (ru != NULL)
  572.                     getrusage(p, RUSAGE_BOTH, ru);
  573.                 flag = p->pid;
  574.                 if (stat_addr)
  575.                     put_fs_long(p->exit_code, stat_addr);
  576.                 if (p->p_opptr != p->p_pptr) {
  577.                     REMOVE_LINKS(p);
  578.                     p->p_pptr = p->p_opptr;
  579.                     SET_LINKS(p);
  580.                     notify_parent(p);
  581.                 } else
  582.                     release(p);
  583. #ifdef DEBUG_PROC_TREE
  584.                 audit_ptree();
  585. #endif
  586.                 retval = flag;
  587.                 goto end_wait4;
  588.             default:
  589.                 continue;
  590.         }
  591.     }
  592.     if (flag) {
  593.         retval = 0;
  594.         if (options & WNOHANG)
  595.             goto end_wait4;
  596.         current->state=TASK_INTERRUPTIBLE;
  597.         schedule();
  598.         current->signal &= ~(1<<(SIGCHLD-1));
  599.         retval = -ERESTARTSYS;
  600.         if (current->signal & ~current->blocked)
  601.             goto end_wait4;
  602.         goto repeat;
  603.     }
  604.     retval = -ECHILD;
  605. end_wait4:
  606.     remove_wait_queue(¤t->wait_chldexit,&wait);
  607.     return retval;
  608. }
  609.  
  610. /*
  611.  * sys_waitpid() remains for compatibility. waitpid() should be
  612.  * implemented by calling sys_wait4() from libc.a.
  613.  */
  614. asmlinkage int sys_waitpid(pid_t pid,unsigned long * stat_addr, int options)
  615. {
  616.     return sys_wait4(pid, stat_addr, options, NULL);
  617. }
  618.